﻿namespace Anno.DynamicApi
{
    public class ApiFieldInfo
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 字段类型
        /// </summary>
        public string TypeName { get; set; }
        /// <summary>
        /// 是否允许空
        /// </summary>
        public bool? AllowNullable { get; set; }
        /// <summary>
        /// 默认值
        /// </summary>
        public object DefaultValue { get; set; }
        string _SqlName = "";
        /// <summary>
        /// 对应数据库字段名
        /// </summary>
        public string SqlName { get { return _SqlName.IsNullOrEmpty() ? Name : _SqlName; } set { _SqlName = value; } }

        string _SqlTypeName = "";
        /// <summary>
        /// 对应数据库字段类型
        /// </summary>
        public string SqlTypeName { get { return _SqlTypeName.IsNullOrEmpty() ? TypeName : _SqlTypeName; } set { _SqlTypeName = value; } }
        /// <summary>
        /// 标题/中文名
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }

        string _ValuePath = "";
        /// <summary>
        /// 入参取值路径 （如：id 、userInfo.id）
        /// </summary>
        public string ValuePath { get { return _ValuePath.IsNullOrEmpty() ? Name : _ValuePath; } set { _ValuePath = value; } }

        /// <summary>
        /// 是否有下级入参
        /// </summary>
        public bool HasSub { get { return SubFieldInfoList?.Count > 0; } }
        /// <summary>
        /// 入参集合
        /// </summary>
        public System.Collections.Generic.List<ApiFieldInfo> SubFieldInfoList { get; set; }

        public ApiFieldInfo() { }
        public ApiFieldInfo(System.Xml.XmlNode xmlNode)
        {
            if (xmlNode == null | xmlNode.NodeType == System.Xml.XmlNodeType.Comment)
            {
                return;
            }
            Init(xmlNode);
        }
        public ApiFieldInfo(Newtonsoft.Json.Linq.JToken xmlNode)
        {
            if (xmlNode == null)
            {
                return;
            }
            Init(xmlNode);
        }
        public void Init(System.Xml.XmlNode parameterNode)
        {
            try
            {
                if (parameterNode == null || parameterNode.NodeType == System.Xml.XmlNodeType.Comment)
                {
                    return;
                }

                Name = parameterNode.GetXmlNodeAttributeValue("Name");
                SqlName = parameterNode.GetXmlNodeAttributeValue("SqlName", Name);
                TypeName = parameterNode.GetXmlNodeAttributeValue("TypeName", "string");
                DefaultValue = parameterNode.GetXmlNodeAttributeValue("DefaultValue");
                AllowNullable = parameterNode.GetXmlNodeAttributeValue("AllowNullable", "true").ChangeType<bool>();
                Title = parameterNode.GetXmlNodeAttributeValue("Title",Name);
                Remark = parameterNode.GetXmlNodeAttributeValue("Remark", Title);
                SqlTypeName = parameterNode.GetXmlNodeAttributeValue("SqlTypeName", TypeName);
                ValuePath = parameterNode.GetXmlNodeAttributeValue("ValuePath", Name);
            }
            catch { }
        }
		 public void Init(Newtonsoft.Json.Linq.JToken parameterNode)
        {
            try
            {
                if (parameterNode == null)
                {
                    return;
                }
                Name = parameterNode.GetXmlNodeAttributeValue("Name");
                SqlName = parameterNode.GetXmlNodeAttributeValue("SqlName", Name);
                TypeName = parameterNode.GetXmlNodeAttributeValue("TypeName", "string");
                DefaultValue = parameterNode.GetXmlNodeAttributeValue("DefaultValue");
                AllowNullable = parameterNode.GetXmlNodeAttributeValue("AllowNullable", "true").ChangeType<bool>();
                Title = parameterNode.GetXmlNodeAttributeValue("Title", Name);
                Remark = parameterNode.GetXmlNodeAttributeValue("Remark", Title);
                SqlTypeName = parameterNode.GetXmlNodeAttributeValue("SqlTypeName", TypeName);
                ValuePath = parameterNode.GetXmlNodeAttributeValue("ValuePath", Name);
            }
            catch { }
        }
        /// <summary>
        /// 返回JSON对象
        /// </summary>
        public virtual object JObject
        {
            get
            {
                var dyret = DicAttrs;

                var subFieldInfoList = new System.Collections.Generic.List<object>();
                SubFieldInfoList?.ForEach(ext => subFieldInfoList.Add(ext.JObject));
                dyret["SubFieldInfoList"] = subFieldInfoList;

                dyret["HasSub"] = HasSub;
                return dyret;

                //var subFieldInfoList = new System.Collections.Generic.List<object>();
                //SubFieldInfoList?.ForEach(ext => subFieldInfoList.Add(ext.JObject));
                //return new
                //{
                //    Name,
                //    SqlName,
                //    TypeName,
                //    DefaultValue,
                //    AllowNullable,
                //    Title,
                //    Remark,
                //    SqlTypeName,
                //    ValuePath,
                //    HasSub,
                //    SubFieldInfoList = subFieldInfoList
                //};
            }
        }
        public virtual System.Collections.Generic.IDictionary<string, object> DicAttrs
        {
            get
            {
                var ret = new System.Dynamic.ExpandoObject();
                System.Collections.Generic.IDictionary<string, object> dyret = ret;

                //var subFieldInfoList = new System.Collections.Generic.List<System.Collections.Generic.IDictionary<string, object>>();
                //SubFieldInfoList?.ForEach(ext => subFieldInfoList.Add(ext.DicAttrs));

                dyret["Name"] = Name;
                dyret["SqlName"] = SqlName;
                dyret["TypeName"] = TypeName;
                dyret["DefaultValue"] = DefaultValue;
                dyret["AllowNullable"] = AllowNullable;
                dyret["Title"] = Title;
                dyret["Remark"] = Remark;
                dyret["SqlTypeName"] = SqlTypeName;
                dyret["ValuePath"] = ValuePath;
                //dyret["SubFieldInfoList"] = subFieldInfoList;

                //dyret["HasSub"] = HasSub;

                return ret;
            }
        }
    }
}
